package modellayer;

import java.util.ArrayList;
import controllayer.UpdateIncompleteException;

/**
 * The Class PersonContainer.
 *@author Benjamin Ørskov
 *@author Lasse Lambertsen
 *@author Ole Kirk
 *@author Søren Søgaard
 */
public class PersonContainer {

	/** The instance. */
	private static PersonContainer instance;

	/** The persons. */
	private ArrayList<Person> persons;

	/** The salesman id. */
	private int salesmanID = 1;

	/**
	 * Instantiates a new person container.
	 */
	private PersonContainer() {
		persons = new ArrayList<Person>();
	}

	/**
	 * Gets the single instance of PersonContainer.
	 *
	 * @return single instance of PersonContainer
	 */
	public static PersonContainer getInstance() {
		if (instance == null) {
			instance = new PersonContainer();
		}
		return instance;
	}

	/**
	 * Gets the persons.
	 *
	 * @return the persons
	 */
	public ArrayList<Person> getPersons() {
		return persons;
	}

	/**
	 * Gets the salesman array.
	 *
	 * @return the salesman array
	 */
	public ArrayList<Salesman> getSalesmanArray() {
		ArrayList<Salesman> salesmanArray = new ArrayList<Salesman>();
		int size = persons.size();
		for (int i = 0; i < size; i++) {
			if (persons.get(i) instanceof Salesman) {
				salesmanArray.add((Salesman) persons.get(i));
			}
		}
		return salesmanArray;
	}

	/**
	 * Gets the salesman id.
	 *
	 * @return the salesmanID
	 */
	public int getSalesmanID() {
		return salesmanID;
	}

	/**
	 * Find customer.
	 *
	 * @param phone
	 *            the phone
	 * @return the person
	 */
	public Person findCustomer(String phone) {
		Person returnPerson = null;
		boolean found = false;
		int size = persons.size();
		for (int i = 0; i < size && !found; i++) {
			if (persons.get(i) instanceof Customer) {
				if (persons.get(i).getPhone().equals(phone)) {
					returnPerson = persons.get(i);
					found = true;
				}
			}
		}
		if (returnPerson == null) {
			throw new NullPointerException("Kunde ikke fundet!");
		}
		return returnPerson;
	}

	/**
	 * Find salesman.
	 *
	 * @param id
	 *            the id
	 * @return the person
	 */
	public Person findSalesman(int id) {
		Person returnPerson = null;
		boolean found = false;
		int size = persons.size();
		for (int i = 0; i < size && !found; i++) {
			if (persons.get(i) instanceof Salesman) {
				if (((Salesman) persons.get(i)).getId() == id) {
					returnPerson = persons.get(i);
					found = true;
				}
			}
		}
		if (returnPerson == null) {
			throw new NullPointerException("Sælger ikke fundet!");
		}
		return returnPerson;
	}

	/**
	 * Find business customer.
	 *
	 * @param cvr
	 *            the cvr
	 * @return the person
	 */
	public Person findBusinessCustomer(String cvr) {
		Person returnPerson = null;
		boolean found = false;
		int size = persons.size();
		for (int i = 0; i < size && !found; i++) {
			if (persons.get(i) instanceof BusinessCustomer) {
				if (((BusinessCustomer) persons.get(i)).getCvr().equals(cvr)) {
					returnPerson = persons.get(i);
					found = true;
				}
			}
		}
		if (returnPerson == null) {
			throw new NullPointerException("Erhvervskunde ikke fundet!");
		}
		return returnPerson;
	}

	/**
	 * Removes the person.
	 *
	 * @param tempP
	 *            the temp p
	 */
	public void removePerson(Person tempP) {
		persons.remove(tempP);
	}

	/**
	 * Update standard.
	 *
	 * @param tempP
	 *            the temp p
	 * @param name
	 *            the name
	 * @param address
	 *            the address
	 * @param city
	 *            the city
	 * @param email
	 *            the email
	 * @param phone
	 *            the phone
	 */
	private void updateStandard(Person tempP, String name, String address,
			String city, String email, String phone) {
		tempP.setName(name);
		tempP.setAddress(address);
		tempP.setCity(city);
		tempP.setEmail(email);
		tempP.setPhone(phone);
	}

	/**
	 * Update salesman.
	 *
	 * @param tempP
	 *            the temp p
	 * @param name
	 *            the name
	 * @param address
	 *            the address
	 * @param city
	 *            the city
	 * @param email
	 *            the email
	 * @param phone
	 *            the phone
	 * @param paycheck
	 *            the paycheck
	 * @param bonus
	 *            the bonus
	 */
	public void updateSalesman(Person tempP, String name, String address,
			String city, String email, String phone, Double paycheck,
			Double bonus) {
		updateStandard(tempP, name, address, city, email, phone);
		((Salesman) tempP).setPaycheck(paycheck);
		((Salesman) tempP).setBonus(bonus);
		if (tempP.getAddress().compareTo(address) != 0
				|| tempP.getCity().compareTo(city) != 0
				|| tempP.getName().compareTo(name) != 0
				|| tempP.getPhone().compareTo(phone) != 0
				|| tempP.getEmail().compareTo(email) != 0
				|| ((Salesman) tempP).getBonus().compareTo(bonus) != 0
				|| ((Salesman) tempP).getPaycheck().compareTo(paycheck) != 0) {
			throw new UpdateIncompleteException(
					"Sælger blev ikke redigeret korrekt!");
		}
	}

	/**
	 * Update business customer.
	 *
	 * @param tempP
	 *            the temp p
	 * @param name
	 *            the name
	 * @param address
	 *            the address
	 * @param city
	 *            the city
	 * @param email
	 *            the email
	 * @param phone
	 *            the phone
	 * @param cvr
	 *            the cvr
	 * @param discount
	 *            the discount
	 */
	public void updateBusinessCustomer(Person tempP, String name,
			String address, String city, String email, String phone,
			String cvr, Double discount) {
		updateStandard(tempP, name, address, city, email, phone);
		((BusinessCustomer) tempP).setCvr(cvr);
		((BusinessCustomer) tempP).setDiscount(discount);
		if (tempP.getAddress().compareTo(address) != 0
				|| tempP.getCity().compareTo(city) != 0
				|| tempP.getName().compareTo(name) != 0
				|| tempP.getPhone().compareTo(phone) != 0
				|| tempP.getEmail().compareTo(email) != 0
				|| ((BusinessCustomer) tempP).getCvr().compareTo(cvr) != 0
				|| ((BusinessCustomer) tempP).getDiscount().compareTo(discount) != 0) {
			throw new UpdateIncompleteException(
					"Kunde blev ikke redigeret korrekt!");
		}
	}

	/**
	 * Update customer.
	 *
	 * @param tempP
	 *            the temp p
	 * @param name
	 *            the name
	 * @param address
	 *            the address
	 * @param city
	 *            the city
	 * @param email
	 *            the email
	 * @param phone
	 *            the phone
	 */
	public void updateCustomer(Person tempP, String name, String address,
			String city, String email, String phone) {
		updateStandard(tempP, name, address, city, email, phone);
		if (tempP.getAddress().compareTo(address) != 0
				|| tempP.getCity().compareTo(city) != 0
				|| tempP.getName().compareTo(name) != 0
				|| tempP.getPhone().compareTo(phone) != 0
				|| tempP.getEmail().compareTo(email) != 0) {
			throw new UpdateIncompleteException(
					"Kunde blev ikke redigeret korrekt!");
		}
	}

	/**
	 * Creates the salesman.
	 *
	 * @param name
	 *            the name
	 * @param address
	 *            the address
	 * @param city
	 *            the city
	 * @param email
	 *            the email
	 * @param phone
	 *            the phone
	 * @param paycheck
	 *            the paycheck
	 * @return the string
	 */
	public String createSalesman(String name, String address, String city,
			String email, String phone, Double paycheck) {
		Salesman s = new Salesman(name, address, city, email, phone,
				salesmanID, paycheck);
		persons.add(s);
		String returnString = "Sælger " + name + " oprettet med ID: "
				+ salesmanID;
		salesmanID++;
		return returnString;
	}

	/**
	 * Creates the business customer.
	 *
	 * @param name
	 *            the name
	 * @param address
	 *            the address
	 * @param city
	 *            the city
	 * @param email
	 *            the email
	 * @param phone
	 *            the phone
	 * @param cvr
	 *            the cvr
	 * @param discount
	 *            the discount
	 */
	public void createBusinessCustomer(String name, String address,
			String city, String email, String phone, String cvr, Double discount) {
		BusinessCustomer bc = new BusinessCustomer(name, address, city, email,
				phone, cvr, discount);
		persons.add(bc);
	}

	/**
	 * Creates the customer.
	 *
	 * @param name
	 *            the name
	 * @param address
	 *            the address
	 * @param city
	 *            the city
	 * @param email
	 *            the email
	 * @param phone
	 *            the phone
	 */
	public void createCustomer(String name, String address, String city,
			String email, String phone) {
		Customer c = new Customer(name, address, city, email, phone);
		persons.add(c);
	}
}